<?php

namespace Home\Controller;

use Think\Controller;

use Home\Lib\Uploader;

/**
 * 各种信息表
 */
class InfoController extends Controller
{
    public function index()
    {
        $this->ajaxReturn([]);
    }

    /**
     * 根据 batch, offset, count 获取基本信息列表, 按 id 倒序排序
     * batch 为空时候获取全部批次
     *
     * @uri /api.php?s=/Home/Info/baseList/batch/201601/offset/0/count/50
     */
    public function baseList($batch = '', $offset = 0, $count = 50)
    {
        $offset = (int) $offset;
        $count = (int) $count;

        $condition = '1';
        if (!empty($batch)) {
            $condition = ['batch' => $batch];
        }

        $baseIfo = D('BaseInfo');

        $data = $baseIfo->getList($condition, $offset, $count);
        $total = $baseIfo->getCount($condition);

        if (is_array($data)) {
            $this->ajaxReturn([
                'code' => 'ok',
                'msg' => '获取成功',
                'data' => $data, // 获取到的数据

                // 当前接口需要信息
                'batch' => $batch, // 批次
                'total' => $total, // 总数量
                'count' => count($data), // 当前数据集合的数量
                'offset' => $offset, // 当前分页偏移
            ]);
        } else {
            $this->ajaxReturn([
                'code' => 'fail',
                'msg' => '获取失败',
                'data' => null,
            ]);
        }
    }

    /**
     * 获取最近60个批次名
     */
    public function baseBatchList()
    {
        $data = D('BaseInfo')->getSomeBatchs();

        if (is_array($data)) {
            $this->ajaxReturn([
                'code' => 'ok',
                'msg' => '获取成功',
                'data' => $data, // 获取到的数据
            ]);
        } else {
            $this->ajaxReturn([
                'code' => 'fail',
                'msg' => '获取失败',
                'data' => null,
            ]);
        }
    }

    public function importBaseInfo()
    {
        $uploader = new Uploader([
            'path' => C('UPLOAD_PATH'),
            'types' => [
                // // 'application/vnd.ms-excel' => '.xls',
                'application/octet-stream' => '.xlsx',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx'
            ]
        ]);

        if (!isset($_FILES['baseinfo'])) {
            exit;
        }

        $result = $uploader->uplaod($_FILES['baseinfo']);
        if ($result['code'] == 'ok') {
            die(json_encode([
                'code' => 'ok',
                'msg' => '导入成功',
                'data' => null,
                'count' => D('BaseInfo')->import($result['src'], 'basicInfo'),
            ]));
        } else {
            die(json_encode(array_merge($result, ['data' => null, 'count' => 0])));
        }
    }

    /**
     * 导入定案表数据
     */
    public function importVerdict()
    {
        $uploader = new Uploader([
            'path' => C('UPLOAD_PATH'),
            'types' => [
                // // 'application/vnd.ms-excel' => '.xls',
                'application/octet-stream'=>'.xlsx',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx'
            ]
        ]);

        if (!isset($_FILES['verdict'])) {
            exit;
        }

        $result = $uploader->uplaod($_FILES['verdict']);
        if ($result['code'] == 'ok') {
            die(json_encode([
                'code' => 'ok',
                'msg' => '导入成功',
                'data' => null,
                'count' => D('Verdict')->import($result['src'], 'verdict'),
            ]));
        } else {
            die(json_encode(array_merge($result, ['data' => null, 'count' => 0])));
        }
    }

    public function uploadFile()
    {
        if (!isset($_FILES['verdict-file'])) {
            exit;
        }

        $id = (int) $_POST['xdata'];

        $fileType = pathinfo($_FILES['verdict-file']['name'], PATHINFO_EXTENSION);

        if (in_array($fileType, [
            'php',
            'html',
            'htm',
            'exe',
            'com',
            'vbs',
            'bat'
        ])) {
            exit;
        }

        $types = [$_FILES['verdict-file']['type'] => '.'.$fileType];

        $uploader = new Uploader([
            'path' => C('UPLOAD_PATH'),
            'types' => $types,
        ]);

        $result = $uploader->uplaod($_FILES['verdict-file']);
        if ($result['code'] == 'ok' and D('Verdict')->save(['id' => $id, 'file' => $result['src']])) {
            die(json_encode([
                'code' => 'ok',
                'msg' => '上传成功',
                'data' => null,
                'id' => $id,
                'src' => $result['src'],
            ]));
        } else {
            die(json_encode(array_merge($result, ['data' => null])));
        }
    }
}
